
import Eureka

//添加培训表单


class addTrainController : FormViewController {

    private let waiting=waitingAlertUtil(msg: "请稍后..")

    //传过来的resumeId
    var resumeId:String = String()

	override func viewDidLoad() {
        super.viewDidLoad()

        // 日期转换器
        let DateFormatter=NSDateFormatter()
        DateFormatter.dateFormat="yyyy-MM-dd"

        URLRow.defaultCellUpdate = { cell, row in cell.textField.textColor = .blueColor() }
        LabelRow.defaultCellUpdate = { cell, row in cell.detailTextLabel?.textColor = .orangeColor()  }
        CheckRow.defaultCellSetup = { cell, row in cell.tintColor = .orangeColor() }
        // 设置最大最小日期
        DateRow.defaultRowInitializer = { row in 
            row.maximumDate = DateFormatter.dateFromString("2015-12-31")
            row.minimumDate = DateFormatter.dateFromString("1950-01-01")
        }

        form +++ Section()
            <<< TextRow("trainOrg"){ row in
                row.title = "培训机构名称"
                row.placeholder = "输入名称"
            }
            <<< TextRow("className"){ row in
                row.title = "培训课程名称"
                row.placeholder = "输入名称"
            }
            <<< TextAreaRow("description"){
                $0.title = "详细描述"
                $0.placeholder = "输入详细描述"
            }

        +++ Section() 
            <<< DateRow("startdate"){
                    $0.title = "开始时间"
                    $0.value = DateFormatter.dateFromString("2010-01-01")
                }
            <<< DateRow("enddate"){
                    $0.title = "结束时间"
                    $0.value = DateFormatter.dateFromString("2010-01-01")
                }

        +++ Section()
            <<< ButtonRow() { (row: ButtonRow) -> Void in
                   row.title = "添加培训经历"
                row.cell.tintColor = UIColor.hexStringToColor("#ffffff")
                row.cell.backgroundColor = UIColor.hexStringToColor("#ff4081")
                }  .onCellSelection({ (cell, row) in
                    self.submitForm()
                })


    }


    private func submitForm(){
        // 等待提示
//        waiting.show(self)
        //获取表单结果
        let dict = form.values()

        //由segue传过来的resumeId
        let resumeId = self.resumeId
        
        let trainOrg = dict["trainOrg"] as! String
        
        let className = dict["className"] as! String
        let description = dict["description"] as! String

        let startdate = dict["startdate"] as! NSDate
        let enddate = dict["enddate"] as! NSDate

        

        resumeManager.AddTrainExperience(resumeId,
                             startdate:startdate,
                             enddate:enddate,
                             trainOrg:trainOrg,
                             className:className,
                             description:description,
                             successCallback: { (msg) in
                    //            添加信息ok
//                                self.waiting.stop()
                    //            退出
                                self.navigationController?.popViewControllerAnimated(true)
                                }) { (error) in
                                    self.waiting.stop()
                                    warningAlertUtil(title: "错误", msg: error).show(self)
                            }
    }

}


